package com.example.controller;

import com.example.entity.BaseEntity;
import com.example.service.BaseService;
import com.example.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author chentao
 * @create 2023/3/11 15:53
 * @since 1.0.0
 */
public class BaseController<S extends BaseService<T>, T extends BaseEntity> {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private S service;

    @PostMapping("getPage")
    public Result getPage(@RequestBody T entity) {
        try {
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @PostMapping
    public Result save(@RequestBody T entity) {
        try {
            T rtn = this.service.add(entity);
            return Result.ok(rtn);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping
    public Result deleteById(@RequestParam Long id) {
        try {
            this.service.deleteById(id);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @GetMapping
    public Result getById(@RequestParam Long id) {
        try {
            T t = this.service.getById(id);
            return Result.ok(t);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}